/**
 * 
 * 起迪科技 Copyright (c) 2014-2018 QiDi,Inc.All Rights Reserved.
 */
package cn.qidisoft.edu.hzjt.service.impl;

import java.io.File;
import java.util.List;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.qidisoft.core.common.code.QdfRespCode;
import cn.qidisoft.core.common.exception.dao.DBException;
import cn.qidisoft.core.common.log.LoggerAdapter;
import cn.qidisoft.core.common.log.LoggerAdapterFactory;
import cn.qidisoft.core.common.pagination.Page;
import cn.qidisoft.core.common.pagination.PaginationBean;
import cn.qidisoft.core.util.uuid.UUIDGenerator;
import cn.qidisoft.edu.hzjt.constants.Constant;
import cn.qidisoft.edu.hzjt.dao.CommunityAmateurSchoolAttMapper;
import cn.qidisoft.edu.hzjt.dao.CommunityAmateurSchoolMapper;
import cn.qidisoft.edu.hzjt.dao.CommunityAmateurSchoolStudentMapper;
import cn.qidisoft.edu.hzjt.model.CommunityAmateurSchool;
import cn.qidisoft.edu.hzjt.model.CommunityAmateurSchoolAtt;
import cn.qidisoft.edu.hzjt.model.CommunityAmateurSchoolStudent;
import cn.qidisoft.edu.hzjt.service.ICommunityAmateurSchoolService;
import cn.qidisoft.edu.hzjt.utils.FileTool;
import cn.qidisoft.edu.hzjt.utils.LogType;

/**
 * 
 * 
 * @author Administrator
 * @version $Id: BjxxClassMaterialProjectServiceImpl.java, v 0.1 2018年10月29日 下午1:14:36 hhl Exp $
 */
@Service("communityAmateurSchoolService")
public class CommunityAmateurSchoolServiceImpl implements ICommunityAmateurSchoolService {

    protected static final LoggerAdapter        LOGGER = LoggerAdapterFactory
        .getLogger(LogType.HZJT_LOGGER.val);

    @Autowired
    private CommunityAmateurSchoolMapper        communityAmateurSchoolMapper;

    @Autowired
    private CommunityAmateurSchoolAttMapper     communityAmateurSchoolAttMapper;

    @Autowired
    private CommunityAmateurSchoolStudentMapper communityAmateurSchoolStudentMapper;

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#queryList(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    public List<CommunityAmateurSchool> queryList(CommunityAmateurSchool data) {
        try {
            return communityAmateurSchoolMapper.queryList(data);
        } catch (Exception e) {
            LOGGER.error("queryList [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#queryByPK(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    public CommunityAmateurSchool queryByPK(CommunityAmateurSchool data) {
        try {
            return communityAmateurSchoolMapper.queryByPK(data);
        } catch (Exception e) {
            LOGGER.error("queryByPK [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#add(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    @Transactional
    public String add(CommunityAmateurSchool data) {
        String uuid = UUIDGenerator.genReqID();
        try {
            data.setGid(uuid);

            communityAmateurSchoolMapper.add(data);
            List<CommunityAmateurSchoolStudent> list = data.getCommunityAmateurSchoolStudent();
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    CommunityAmateurSchoolStudent communityAmateurSchoolStudent = list.get(i);
                    communityAmateurSchoolStudent.setParentid(data.getDxid());
                    communityAmateurSchoolStudentMapper.add(communityAmateurSchoolStudent);
                }
            }

        } catch (Exception e) {
            LOGGER.error("add [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
        return uuid;
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#update(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    @Transactional
    public void update(CommunityAmateurSchool data) {
        try {

            communityAmateurSchoolMapper.update(data);

            CommunityAmateurSchoolStudent att = new CommunityAmateurSchoolStudent();
            att.setParentid(data.getDxid());
            communityAmateurSchoolStudentMapper.delete(att);

            List<CommunityAmateurSchoolStudent> list = data.getCommunityAmateurSchoolStudent();
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    CommunityAmateurSchoolStudent communityAmateurSchoolStudent = list.get(i);
                    communityAmateurSchoolStudent.setParentid(data.getDxid());
                    communityAmateurSchoolStudentMapper.add(communityAmateurSchoolStudent);
                }
            }

        } catch (Exception e) {
            LOGGER.error("update [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#delete(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    @Transactional
    public void delete(CommunityAmateurSchool data) {
        try {
            for (String s : data.getIds()) {
                //删除团校参与的学生
                CommunityAmateurSchoolStudent att = new CommunityAmateurSchoolStudent();
                att.setParentid(Integer.valueOf(s));
                communityAmateurSchoolStudentMapper.delete(att);

                CommunityAmateurSchoolAtt data2 = new CommunityAmateurSchoolAtt();
                data2.setParentid(Integer.valueOf(s));
                List<CommunityAmateurSchoolAtt> communityAmateurSchoolAttList = communityAmateurSchoolAttMapper
                    .queryList(data2);
                if (communityAmateurSchoolAttList != null
                    && !communityAmateurSchoolAttList.isEmpty()) {
                    for (CommunityAmateurSchoolAtt r : communityAmateurSchoolAttList) {
                        String upload = Constant.UPLOADPATH;
                        int idlength = r.getFileid().lastIndexOf("");
                        int pos = r.getFilepath().lastIndexOf(".");
                        String filepath = r.getFilepath().substring(0, pos - idlength - 1);
                        File file = new File(upload + File.separator + filepath);
                        /* if (file.isDirectory()) {//判断file是否是文件目录 若是返回TRUE  
                            String name[] = file.list();//name存储file文件夹中的文件名  
                            for (int i = 0; i < name.length; i++) {
                                File f = new File(upload + File.separator + filepath, name[i]);//此时就可得到文件夹中的文件  
                                f.delete();//删除文件  
                            }
                            FileUtils.forceDelete(new File(upload + File.separator + filepath)); //最后删除文件目录
                        }*/
                        FileTool.deleteFile(file);
                    }
                }

                //删除附件
                CommunityAmateurSchoolAtt communityAmateurSchoolAtt = new CommunityAmateurSchoolAtt();
                communityAmateurSchoolAtt.setParentid(Integer.valueOf(s));
                communityAmateurSchoolAttMapper.delete(communityAmateurSchoolAtt);
                //删除主表
                data.setDxid(Integer.valueOf(s));
                communityAmateurSchoolMapper.delete(data);
            }

        } catch (Exception e) {
            LOGGER.error("delete [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#queryPageList(cn.qidisoft.microClass.resource.model.basis.Catalog, cn.qidisoft.core.common.pagination.Page)
     */
    @Override
    public PaginationBean<CommunityAmateurSchool> queryPageList(CommunityAmateurSchool data,
                                                                Page page) {
        try {

            int c = communityAmateurSchoolMapper.queryPageListcount(data);
            int totalRecords = 0;
            totalRecords = (int) c;
            PaginationBean<CommunityAmateurSchool> pageHolder = new PaginationBean<CommunityAmateurSchool>(
                page, totalRecords);
            List<CommunityAmateurSchool> resultList = communityAmateurSchoolMapper.queryPageList(
                data, new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                    pageHolder.getPageSize()));
            pageHolder.setPageList(resultList);
            return pageHolder;
        } catch (Exception e) {
            LOGGER.error("queryPageList [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#validation(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    public int validation(CommunityAmateurSchool data) {
        try {
            return communityAmateurSchoolMapper.validation(data);
        } catch (Exception e) {
            e.printStackTrace();
            return -99;
        }
    }

    /** 
     * @see cn.qidisoft.edu.hzjt.service.ICommunityAmateurSchoolService#export(cn.qidisoft.edu.hzjt.model.CommunityAmateurSchool)
     */
    @Override
    public List<CommunityAmateurSchool> export(CommunityAmateurSchool data) {
        try {
            return communityAmateurSchoolMapper.export(data);
        } catch (Exception e) {
            LOGGER.error("queryByPK [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

}
